package com.ruoyi.spider.spiderConfig.processor;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.spider.domain.ProcessorConfig;
import com.ruoyi.spider.domain.SpiderArticle;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author xgt(小光头)
 * @version 1.0
 * @date 2021-1-15 20:10
 */
public abstract  class AbstractProcessor implements PageProcessor {
    protected Site site;
    protected ProcessorConfig processorConfig;


    public AbstractProcessor(ProcessorConfig processorConfig) {
        this.processorConfig = processorConfig;
    }

    public AbstractProcessor(Site site, ProcessorConfig processorConfig) {
        this.site = site;
        this.processorConfig = processorConfig;
    }

    /**
     * 程序入口方法
     * @return 返回列表
     */
    public abstract CopyOnWriteArrayList<LinkedHashMap<String, String>> execute();

    @Override
    public void process(Page page) {
        Html html = page.getHtml();

        if(!page.getUrl().equals("https://www.x6d.com/html/34.html")){

            List<String> LinkLists = page.getHtml().xpath("//ul[@class='list-ul']/*").links().all();

            if (LinkLists.size() == 0) {
                SpiderArticle spiderArticle = new SpiderArticle();

                //标题
                String title = html.xpath("h1[@class='article-title']/text()").toString();
                //内容
                String content = html.xpath("div[@class='article-content']").toString();
                //小标题
                String contenttype = html.xpath("b[@class='bq-wg']/a/text()").toString();

                HashMap<String, String> stringStringHashMap = new HashMap<>();
                stringStringHashMap.put("weburl",page.getUrl().toString());
                stringStringHashMap.put("title",title);
                stringStringHashMap.put("content",content);
                stringStringHashMap.put("contenttype",contenttype);

//                spiderArticle.setWebUrl(page.getUrl().toString());
//                spiderArticle.setTitle(title);
//                spiderArticle.setContent(content);
               // spiderArticle.

                page.putField("spiderDataMap", JSON.toJSONString(stringStringHashMap));

            }else{
                for (String linkList : LinkLists) {
                    if(!StrUtil.isBlank(linkList)){
                        //加入队列
                        page.addTargetRequest(linkList);
                    }
                }

            }
        }



    }
    /**
     * 自定义管道的处理方法
     *
     * @param resultItems     收集数据
     * @param datas 数据集合
     */
    final void processData(ResultItems resultItems, List<LinkedHashMap<String, String>> datas, Spider spider) {
        if (null == spider) {
            return;
        }
        Map<String, Object> map = resultItems.getAll();
        if (CollectionUtil.isEmpty(map)) {
            return;
        }
        LinkedHashMap<String, String> tempMap=new LinkedHashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            tempMap.put(entry.getKey(),entry.getValue()==null?"":entry.getValue().toString());
        }
        datas.add(tempMap);
    }
    @Override
    public Site getSite() {
        return site;
    }
}
